package me.zhengjie.spider.manager;

import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.PoolObjectException;
import me.zhengjie.spider.common.SpiderAttributeBean;
import me.zhengjie.spider.domain.comic.ComicBean;
import me.zhengjie.spider.domain.comic.ComicRuleBean;
import me.zhengjie.spider.domain.video.VideoBean;
import me.zhengjie.spider.domain.video.VideoRuleBean;
import me.zhengjie.spider.pool.DownloaderObjectPool;
import me.zhengjie.spider.util.pipeline.SpiderComicContentPipeline;
import me.zhengjie.spider.util.pipeline.SpiderComicDetailPipeline;
import me.zhengjie.spider.util.pipeline.SpiderVideoDetailPipeline;
import me.zhengjie.spider.util.processor.SpiderComicContentPageProcessor;
import me.zhengjie.spider.util.processor.SpiderComicDetailPageProcessor;
import me.zhengjie.spider.util.processor.SpiderVideoDetailPageProcessor;
import me.zhengjie.spider.util.service.FileDownloader;
import me.zhengjie.utils.SpringContextHolder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

/**
 * 异步管理类
 *
 * @author Administrator
 */
@Component
@RequiredArgsConstructor
public class SpiderAsyncManager {

    private final DownloaderObjectPool downloaderObjectPool;

    @Async
    public void afterMath(VideoBean videoBean, VideoRuleBean videoRuleBean, SpiderAttributeBean spiderAttributeBean) {
        SpiderVideoDetailPageProcessor videoDetailPageProcessor = SpringContextHolder.getBean(SpiderVideoDetailPageProcessor.class);
        SpiderVideoDetailPipeline videoDetailPipeline = SpringContextHolder.getBean(SpiderVideoDetailPipeline.class);
        videoDetailPageProcessor.setRuleBean(videoRuleBean);
        videoDetailPipeline.setBean(videoBean);
        Spider.create(videoDetailPageProcessor).addUrl(videoRuleBean.getDomain() + videoBean.getHref())
                .addPipeline(videoDetailPipeline).thread(spiderAttributeBean.getThread()).run();
    }

    @Async
    public void downloadImg(String url, String localPath) {
        FileDownloader fileDownloader;
        try {
            fileDownloader = downloaderObjectPool.borrowObject();
        } catch (Exception e) {
            throw new PoolObjectException("从获取DownloaderObjectPool获取对象失败");
        }
        if (fileDownloader != null) {
            // 归还对象到对象池
            fileDownloader.downloadJpg(url, localPath);
            downloaderObjectPool.returnObject(fileDownloader);
        }
    }

    @Async
    public void downloadM3U8(String url, String localPath, String tsNameRule) {
        FileDownloader fileDownloader;
        try {
            fileDownloader = downloaderObjectPool.borrowObject();
        } catch (Exception e) {
            throw new PoolObjectException("从获取DownloaderObjectPool获取对象失败");
        }
        if (fileDownloader != null) {
            // 归还对象到对象池
            fileDownloader.downloadM3U8(url, localPath, tsNameRule);
            downloaderObjectPool.returnObject(fileDownloader);
        }
    }

    @Async
    public void afterComicMath(ComicBean comicBean, ComicRuleBean comicRuleBean, SpiderAttributeBean spiderAttributeBean) {
        SpiderComicDetailPageProcessor comicDetailPageProcessor = SpringContextHolder.getBean(SpiderComicDetailPageProcessor.class);
        SpiderComicDetailPipeline comicDetailPipeline = SpringContextHolder.getBean(SpiderComicDetailPipeline.class);
        comicDetailPageProcessor.setRuleBean(comicRuleBean);
        comicDetailPipeline.setBean(comicBean);
        Spider.create(comicDetailPageProcessor).addUrl(comicRuleBean.getDomain() + comicBean.getHref())
                .addPipeline(comicDetailPipeline).thread(spiderAttributeBean.getThread()).run();
    }

    @Async
    public void spiderComicContent(ComicBean comicBean, ComicRuleBean comicRuleBean, SpiderAttributeBean spiderAttributeBean) {
        SpiderComicContentPageProcessor comicContentPageProcessor = SpringContextHolder.getBean(SpiderComicContentPageProcessor.class);
        SpiderComicContentPipeline comicContentPipeline = SpringContextHolder.getBean(SpiderComicContentPipeline.class);
        comicContentPageProcessor.setRuleBean(comicRuleBean);
        comicContentPipeline.setBean(comicBean);
        comicContentPipeline.setRuleBean(comicRuleBean);
        comicContentPipeline.setAttributeBean(spiderAttributeBean);
        Spider.create(comicContentPageProcessor).addUrl(comicRuleBean.getDomain() + comicBean.getHref())
                .addPipeline(comicContentPipeline).thread(spiderAttributeBean.getThread()).run();
    }
}
